#!/bin/bash

#echo carefully review/change/config this script, before running it 		# delete this line
#echo exiting unconfigured script now ... && exit 						# delete this line

cowsay start
rootname=$(date +"%m%d%Y_%H%M%S_$HOSTNAME")
folder="$rootname"_configs
mkdir ~/backup/$folder

echo saving to ~/backup/$folder

# smile
cowsay screenshot
scrot scrot.png
mv scrot.png ~/backup/$folder

cowsay wall
# get nitrogen wallpaper location and backup active wallpaper
file=~/.config/nitrogen/bg-saved.cfg
if [ -f $file ] ; 
then 
wallpath=`grep 'file=' $file | cut -c 6-`
echo "$wallpath"
mkdir ~/backup/$folder/wallpaper
cp "$wallpath" ~/backup/$folder/wallpaper
fi

cowsay stuff
# stuff
cp -v ~/.conkyrc ~/backup/$folder
cp -v ~/.bash_aliases ~/backup/$folder
cp -v ~/.bashrc ~/backup/$folder
cp -v ~/.rtorrent.rc ~/backup/$folder

cowsay etc 
# etc all
cp -r /etc/ ~/backup/$folder

cowsay .config and .fonts 
# folders
cp -r ~/.config/ ~/backup/$folder
cp -r ~/.fonts/ ~/backup/$folder
cowsay .icons 
cp -r ~/.icons/ ~/backup/$folder
cowsay .themes
cp -r ~/.themes/ ~/backup/$folder
cp -r ~/.mplayer/ ~/backup/$folder
cp -r ~/.xchat2/ ~/backup/$folder
cp -r ~/bin/ ~/backup/$folder

cowsay packages 
# installed packages
dpkg --get-selections > ~/backup/$folder/installed_software.log

cowsay compress 
# compress all into tar.gz
cd ~/backup/
tar -acf $folder.tar.gz $folder/


# remove tmp folder
rm -r ~/backup/$folder

# cowsay
cowsay main backup done : $folder.tar.gz

# and the www stuff and all the mysql databases
cowsay www stuff and mysql

echo "mysql user:"
read mysql_user
echo "mysql password:"
stty_orig=`stty -g` # save original terminal setting.
stty -echo          # turn-off echoing.
read mysql_pass     # read the password
stty $stty_orig     # restore terminal setting.

wwwfolder="$rootname"_www
mkdir ~/backup/$wwwfolder

echo saving to ~/backup/$wwwfolder

cowsay mysqldump
mysqldump --user=$mysql_user --password=$mysql_pass --all-databases --lock-tables=false > ~/backup/$wwwfolder/mysql_backup.sql
cowsay www stuff
cp -r ~/www/ ~/backup/$wwwfolder

# compress all www into tar.gz
cd ~/backup/
tar -acf $wwwfolder.tar.gz $wwwfolder/


# remove tmp www folder
rm -r ~/backup/$wwwfolder

# cowsay
cowsay www backup done : $wwwfolder.tar.gz



# echo
file=~/backup/$folder.tar.gz
if [ -f $file ] ; 
then
ls -lah ~/backup/$folder.tar.gz
fi
file=~/backup/$wwwfolder.tar.gz
if [ -f $file ] ; 
then
ls -lah ~/backup/$wwwfolder.tar.gz
fi

